package com.module.admin.rest.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.module.admin.rest.dao.RestServerDao;
import com.module.admin.rest.pojo.RestServer;
import com.module.admin.rest.service.RestServerService;
import com.system.comm.model.Page;
import com.system.handler.model.ResponseFrame;
import com.system.handler.model.ResponseCode;

/**
 * rest_server的Service
 * @author yuejing
 * @date 2016-12-23 16:43:04
 * @version V1.0.0
 */
@Component
public class RestServerServiceImpl implements RestServerService {

	@Autowired
	private RestServerDao restServerDao;
	
	@Override
	public ResponseFrame save(String serverAddress) {
		ResponseFrame frame = new ResponseFrame();
		RestServer restServer = getByServerAddress(serverAddress);
		if(restServer == null) {
			restServer = new RestServer();
			restServer.setServerAddress(serverAddress);
			restServerDao.save(restServer);
		}
		frame.setBody(restServer);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}

	private RestServer getByServerAddress(String serverAddress) {
		return restServerDao.getByServerAddress(serverAddress);
	}

	@Override
	public RestServer get(Integer rsId) {
		return restServerDao.get(rsId);
	}

	@Override
	public ResponseFrame pageQuery(RestServer restServer) {
		ResponseFrame frame = new ResponseFrame();
		int total = restServerDao.findRestServerCount(restServer);
		List<RestServer> data = null;
		if(total > 0) {
			data = restServerDao.findRestServer(restServer);
		}
		Page<RestServer> page = new Page<RestServer>(restServer.getPage(), restServer.getSize(), total, data);
		frame.setBody(page);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
	
	@Override
	public ResponseFrame delete(Integer rsId) {
		ResponseFrame frame = new ResponseFrame();
		restServerDao.delete(rsId);
		frame.setCode(ResponseCode.SUCC.getCode());
		return frame;
	}
}
